package main.java.service;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import main.java.DAO.IUserInfoBasicDAO;
import main.java.constant.SessionKey;
import main.java.constant.StateEnum;
import main.java.model.MyUserDetails;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.WebAttributes;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

public class AjaxAuthenticationSuccessHandler implements AuthenticationSuccessHandler{
	@Autowired
	@Qualifier("userService")
	UserService userService;
	
	@Override
	public void onAuthenticationSuccess(HttpServletRequest request,
			HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		//output result to response
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter writer = response.getWriter();
		String rtn = StateEnum.SUCCESS.name();
		writer.write(rtn);
		writer.close();
		
		//put userInfoBasic object info into current UserDetails object
		userService.updateCurrentLoginUserInfoBasicModel();
		
		clearAuthenticationAttributes(request);
	}

    /**
     * Removes temporary authentication-related data which may have been stored in the session
     * during the authentication process.
     */
    protected final void clearAuthenticationAttributes(HttpServletRequest request) {
        HttpSession session = request.getSession(false);

        if (session == null) {
            return;
        }

        session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
    }
}
